import type { UserRecord } from '@/DatabaseType/User';
import { ServiceProxy } from '@/constant';
import type { RequestResult } from '@/utils/request';

import { request } from '@umijs/max';
import type { PaginationConfig } from 'antd/lib/pagination';

// 登入，后端留存session
export async function login(
  data: Pick<UserRecord, 'password' | 'username'>,
): Promise<RequestResult<UserRecord>> {
  return request(`${ServiceProxy.DefaultPrefix}/user/login`, {
    method: 'POST',
    data,
  });
}

// 退出登入，后端重新生成session
export async function outLogin(): Promise<RequestResult<void>> {
  return request(`${ServiceProxy.DefaultPrefix}/user/login_info`, {
    method: 'DELETE',
  });
}

// 注册成功，返回注册的用户id
export async function register(
  data: Pick<UserRecord, 'password' | 'username' | 'email'> & {
    roleId: number;
  },
): Promise<RequestResult<number>> {
  return request(`${ServiceProxy.DefaultPrefix}/user/register`, {
    method: 'POST',
    data,
  });
}

// 后端通过前端sessionId校验登入，普通用户不会频繁请求登入态
export async function getLoginInfo(): Promise<RequestResult<UserRecord>> {
  return request(`${ServiceProxy.DefaultPrefix}/user/login_info`, {
    method: 'GET',
    useCache: true,
    ttl: 5 * 60 * 1000, // 五分钟
    maxCache: 10,
  });
}

// 更新用户信息
export async function updateUserInfo(
  data: Pick<UserRecord, 'nickname' | 'email' | 'avatarImage'>,
): Promise<RequestResult> {
  return request(`${ServiceProxy.DefaultPrefix}/user/base_info`, {
    method: 'PUT',
    data,
  });
}

// 修改用户密码
export async function updateUserPassword(
  data: Pick<UserRecord, 'password'> & {
    email: string;
    password: string;
    code?: string;
    oldPassword?: string;
  },
): Promise<RequestResult> {
  return request(`${ServiceProxy.DefaultPrefix}/user/password`, {
    method: 'PUT',
    data,
  });
}

// 向邮箱发起验证码
export async function sendEmailCode(email: string): Promise<RequestResult> {
  return request(`${ServiceProxy.DefaultPrefix}/user/code`, {
    method: 'POST',
    data: {
      email,
    },
  });
}

interface GetUserListParams {
  id: number;
  username: string;
  email: string;
  nickname: string;
}

// 获取用户列表
export async function getUserList(
  params: Partial<GetUserListParams> & PaginationConfig,
): Promise<
  RequestResult<{
    total: number;
    list: UserRecord[];
  }>
> {
  return request(`${ServiceProxy.DefaultPrefix}/user`, {
    method: 'GET',
    params,
  });
}

// 获取用户记录
export async function getUser(
  userId: number,
): Promise<RequestResult<UserRecord>> {
  return request(`${ServiceProxy.DefaultPrefix}/user/${userId}`, {
    method: 'GET',
  });
}

// 删除用户信息
export async function deleteUser(userId: number): Promise<RequestResult<void>> {
  return request(`${ServiceProxy.DefaultPrefix}/user/${userId}`, {
    method: 'DELETE',
  });
}

// 创建用户，返回创建的用户id
export async function addUser(
  data: Pick<UserRecord, 'email' | 'nickname' | 'username' | 'avatarImage'> & {
    roleIds: number[];
  },
): Promise<RequestResult<number>> {
  return request(`${ServiceProxy.DefaultPrefix}/user`, {
    method: 'POST',
    data,
  });
}

// 更新用户
export async function updateUser(
  userId: number,
  data: Pick<UserRecord, 'email' | 'nickname' | 'username' | 'avatarImage'> & {
    roleIds: number[];
  },
): Promise<RequestResult<number>> {
  return request(`${ServiceProxy.DefaultPrefix}/user/${userId}`, {
    method: 'PUT',
    data,
  });
}
